<?php


use App\Library\Utils\Arr;
use Phinx\Seed\AbstractSeed;
use Faker\Factory as FakerFactory;

class ImNoticeSeeder extends AbstractSeed
{
    public function getDependencies(): array
    {
        return [
            'UserSeeder',
            'ImGroupSeeder',
        ];
    }

    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * https://book.cakephp.org/phinx/0/en/seeding.html
     */
    public function run()
    {
        $faker = FakerFactory::create('zh_CN');

        $users = $this->fetchAll('SELECT * FROM kg_user');
        $groups = $this->fetchAll('SELECT * FROM kg_im_group');

        $rows = [];
        for ($i = 0; $i < 2000; $i++) {
            $sender = Arr::rand($users);
            $receiverId = Arr::rand($users)['id'];
            $group = Arr::rand($groups);
            $itemInfo = [
                'sender' => [
                    'id' => $sender['id'],
                    'name' => $sender['name'],
                    'avatar' => $sender['avatar'],
                ],
                'group' => [
                    'id' => $group['id'],
                    'name' => $group['name'],
                ],
                'remark' => $faker->text(10),
                'status' => 'accepted',
            ];
            $rows[] = [
                'sender_id' => $sender['id'],
                'receiver_id' => $receiverId,
                'item_type' => rand(1, 5),
                'item_info' => json_encode($itemInfo, JSON_UNESCAPED_UNICODE),
                'viewed' => rand(0, 1),
                'create_time' => $faker->unixTime,
                'update_time' => $faker->unixTime,
            ];
        }
        $this->table('kg_im_notice')->insert($rows)->saveData();
    }
}
